package com.klasster.recruitmentPlatform.common.util;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.klasster.recruitmentPlatform.common.response.Result;
import com.klasster.recruitmentPlatform.common.response.ResultCode;
import com.klasster.recruitmentPlatform.domain.dto.InterviewDTO;
import com.klasster.recruitmentPlatform.domain.dto.ResumeSubmissionDTO;
import com.klasster.recruitmentPlatform.domain.dto.ResumeSubmissionStepDTO;
import com.klasster.recruitmentPlatform.domain.po.Job;
import com.klasster.recruitmentPlatform.domain.po.ResumeSubmission;
import com.klasster.recruitmentPlatform.domain.po.ResumeSubmissionStep;
import com.klasster.recruitmentPlatform.domain.po.Seeker;
import com.klasster.recruitmentPlatform.domain.vo.ResumeSubmissionProgressVO;
import com.klasster.recruitmentPlatform.domain.vo.ResumeSubmissionStepVO;
import com.klasster.recruitmentPlatform.domain.vo.ResumeSubmissionVO;
import com.klasster.recruitmentPlatform.service.impl.JobServiceImpl;
import com.klasster.recruitmentPlatform.service.impl.ResumeSubmissionServiceImpl;
import com.klasster.recruitmentPlatform.service.impl.ResumeSubmissionStepServiceImpl;
import com.klasster.recruitmentPlatform.service.impl.SeekerServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Component
@RequiredArgsConstructor
public class ResumeSubmissionUtil {

    private final SeekerServiceImpl seekerService;
    private final JobServiceImpl jobService;
    private final ResumeSubmissionServiceImpl resumeSubmissionService;
    private final ResumeSubmissionStepServiceImpl resumeSubmissionStepService;

    /**
     * 投递简历
     */
    public Result submitResume(ResumeSubmissionDTO submissionDTO) {
        submissionDTO.setStep(0);
        submissionDTO.setState(1);
        ResumeSubmission resumeSubmission = BeanUtil.copyProperties(submissionDTO,ResumeSubmission.class);
        resumeSubmissionService.save(resumeSubmission);
        ResumeSubmissionStep resumeSubmissionStep = BeanUtil.copyProperties(resumeSubmission,ResumeSubmissionStep.class);
        resumeSubmissionStep.setState(0);
        resumeSubmissionStep.setDescription("正在核对简历是否符合企业要求...");
        return Result.success(ResultCode.SEEKER_SUBMIT_RESUME_SUCCESS,resumeSubmissionStepService.save(resumeSubmissionStep));
    }


    /**
     * 获取简历投递列表
     */
    public List<ResumeSubmissionVO> getResumeSubmissions(String type, Long id) {
        QueryWrapper<ResumeSubmission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(type,id);
        List<ResumeSubmission> resumeSubmissions = resumeSubmissionService.list(queryWrapper);
        List<ResumeSubmissionVO> resumeSubmissionVOS = BeanUtil.copyToList(resumeSubmissions,ResumeSubmissionVO.class);
        resumeSubmissionVOS.forEach(e -> {
            Job job = jobService.getById(e.getJob_id());
            e.setTitle(job.getTitle());
            Seeker seeker = seekerService.getById(e.getSeeker_id());
            e.setName(seeker.getName());
        });
        return resumeSubmissionVOS;
    }

    /**
     * 获取招聘人员面试安排列表
     */
    public List<ResumeSubmissionStepVO> getInterviewArrangements(Long recruiter_id) {
        QueryWrapper<ResumeSubmissionStep> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("recruiter_id",recruiter_id).eq("state",0);
        List<ResumeSubmissionStep> resumeSubmissionSteps = resumeSubmissionStepService.list(queryWrapper);
        List<ResumeSubmissionStepVO> resumeSubmissionVOS = BeanUtil.copyToList(resumeSubmissionSteps,ResumeSubmissionStepVO.class);
        resumeSubmissionVOS.forEach(e -> {
            Job job = jobService.getById(e.getJob_id());
            e.setTitle(job.getTitle());
            Seeker seeker = seekerService.getById(e.getSeeker_id());
            e.setName(seeker.getName());
        });
        return resumeSubmissionVOS;

    }

    /**
     * 通过面试
     */
    public Result passInterview(ResumeSubmissionStepDTO resumeSubmissionStepDTO) {
        ResumeSubmission resumeSubmission = resumeSubmissionService.getById(resumeSubmissionStepDTO.getSubmission_id());
        int step = resumeSubmission.getStep();
        //通过的是前3面
        if(step<3) {
            resumeSubmission.setState(0);
            resumeSubmission.setStep(step+1);
            resumeSubmissionService.updateById(resumeSubmission);
        }
        //通过最后一面
        else {
            resumeSubmission.setState(2);
            resumeSubmissionService.updateById(resumeSubmission);
        }
        UpdateWrapper<ResumeSubmissionStep> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",resumeSubmissionStepDTO.getId());
        updateWrapper.set("state",1);
        updateWrapper.set("result",resumeSubmissionStepDTO.getResult());
        return Result.success(ResultCode.RECRUITER_SAVE_SUCCESS,resumeSubmissionStepService.update(updateWrapper));
    }

    /**
     * 不通过面试
     */
    public Result failInterview(ResumeSubmissionStepDTO resumeSubmissionStepDTO) {
        ResumeSubmission resumeSubmission = resumeSubmissionService.getById(resumeSubmissionStepDTO.getSubmission_id());
        resumeSubmission.setState(3);
        resumeSubmissionService.updateById(resumeSubmission);
        UpdateWrapper<ResumeSubmissionStep> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",resumeSubmissionStepDTO.getId());
        updateWrapper.set("state",2);
        updateWrapper.set("result",resumeSubmissionStepDTO.getResult());
        return Result.success(ResultCode.RECRUITER_SAVE_SUCCESS,resumeSubmissionStepService.update(updateWrapper));
    }


    /**
     * 安排面试
     */
    public Result arrangeInterview(InterviewDTO interviewDTO) {
        ResumeSubmission resumeSubmission = resumeSubmissionService.getById(interviewDTO.getSubmission_id());
        resumeSubmission.setStep(interviewDTO.getStep());
        resumeSubmission.setState(1);
        resumeSubmissionService.updateById(resumeSubmission);
        ResumeSubmissionStep resumeSubmissionStep = BeanUtil.copyProperties(interviewDTO,ResumeSubmissionStep.class);
        return Result.success(ResultCode.RECRUITER_SAVE_SUCCESS,resumeSubmissionStepService.save(resumeSubmissionStep));
    }

    public List<ResumeSubmissionProgressVO> getResumeSubmissionProgress(Long submission_id) {
        QueryWrapper<ResumeSubmissionStep> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("submission_id",submission_id).orderByAsc("step");
        List<ResumeSubmissionStep> resumeSubmissionSteps = resumeSubmissionStepService.list(queryWrapper);
        return BeanUtil.copyToList(resumeSubmissionSteps,ResumeSubmissionProgressVO.class);
    }
}
